package com.mobvoi.ros;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import com.mobvoi.serialport.controller.MicArrayController;
import com.mobvoi.serialport.controller.MicCallbackParser;
import com.mobvoi.serialport.controller.RobotActionController;
import com.mobvoi.serialport.controller.RosCallbackParser;
import static com.mobvoi.ros.BaseApplication.rosController;
import static com.mobvoi.ros.BaseApplication.micController;
import static com.mobvoi.ros.BaseApplication.uartServiceStart;

public class CoreService extends Service implements RosCallbackParser.RosCallback, MicCallbackParser.MicCallback{

    public static final String MICARRAY_UART="/dev/ttyS4";
    public static final String ROS_UART="/dev/ttyS5";
    private static final int NOTIFICATION_ID = 1;
    private static final String CHANNEL_ID = "my_channel_01";

    private void initUart()
    {
        uartServiceStart=true;
        //初始化串口
        if (rosController == null) {
            rosController = RobotActionController.getInstance();
            //String devname=ofChassis(Build.PRODUCT);
            try {
                rosController.init(
                        115200,
                        ROS_UART,
                        this,
                        BuildConfig.APP_LOG_DIR

                );
                Log.d("uartservice","open" +ROS_UART +"success");
            } catch (Exception e) {
                e.printStackTrace();
                Log.d("uartservice","open" +ROS_UART +"failed");
            }
        }

        //初始化阵列麦串口
        if (micController == null) {
            micController = MicArrayController.getInstance();
            // String devname=ofChassis(Build.PRODUCT);
            try {
                micController.init(
                        115200,
                        MICARRAY_UART,
                        this,
                        BuildConfig.APP_LOG_DIR

                );
                Log.d("uartservice","open" +MICARRAY_UART +"success");
            } catch (Exception e) {
                e.printStackTrace();
                Log.d("uartservice","open" +MICARRAY_UART +"failed");
            }
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("uartservice","uart CoreService onCreate");
        // 创建通知通道，仅在Android 8.0及以上版本需要
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                    "My Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 创建通知
        Notification notification = new Notification.Builder(this, CHANNEL_ID)
                .setContentTitle("Uart Service")
                .setContentText("Running...")
                .setSmallIcon(R.drawable.ic_uart)
                .build();

        // 启动前台服务
        startForeground(NOTIFICATION_ID, notification);

        Log.d("uartservice","uart CoreService onStartCommand");
        initUart();
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        stopForeground(true);
        uartServiceStart=false;
        super.onDestroy();
        Log.d("uartservice","uart CoreService onDestroy");
    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

    @Override
    public void onMicResult(String result_hexstr) {
        Log.d("uartservice","s4 pass to s5");
        rosController.sendCommand(result_hexstr);
    }

    @Override
    public void onResult(String result_hexstr) {
        Log.d("uartservice","s5 pass to s4");
        micController.sendCommand(result_hexstr);
    }
}
